package com.aaa.sys.service.impl;

import com.aaa.common.constant.ConstantUtil;
import com.aaa.common.entity.User;
import com.aaa.common.util.JwtUtils;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.TreeNode;
import com.aaa.sys.dao.MenuDao;
import com.aaa.sys.dao.UserDao;
import com.aaa.sys.service.MenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @FileName: MenuServiceImpl
 * @Description:
 * @Author: zhz
 * @CreateTime: 2025/8/29 11:38
 * @Version: 1.0.0
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuDao menuDao;
    @Resource
    private UserDao userDao;

    @Resource
    private HttpServletRequest request;
    /**
     * 新增菜单
     * @param treeNode 菜单节点对象
     * @return Integer 受影响的行数
     */
    @Override
    public Integer addMenu(TreeNode treeNode) {
        String userId = TokenUtil.getUserId(request);
        User user = userDao.queryById(Integer.valueOf(userId));
        treeNode.setUpdateBy(user.getUserName());
        treeNode.setCreateBy(user.getUserName());
        return menuDao.addMenu(treeNode);
    }

    /**
     * 更新菜单
     * @param treeNode 菜单节点对象
     * @return Integer 受影响的行数
     */
    @Override
    public Integer updateMenu(TreeNode treeNode) {
        return menuDao.updateMenu(treeNode);
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return Integer 受影响的行数
     */
    @Override
    public Integer deleteMenu(Integer id) {
        return menuDao.deleteMenu(id);
    }


    /***
     * -- 一级菜单
     * select * from sys_menu where parent_id=0;
     * -- 一级菜单 系统管理的子菜单
     *
     * select * from sys_menu where parent_id=1;
     * -- 一级菜单 客户管理的子菜单
     *
     * select * from sys_menu where parent_id=119;
     * @return
     */
    @Override
    public List<TreeNode> queryTreeData() {
        //查询数据  所有数据
        List<TreeNode> treeNodeList = menuDao.queryAllMenu();
        //定义返回集合
        List<TreeNode> returnTreeNodeList = new ArrayList<>();
        //判断
        //if(treeNodeList!=null&&treeNodeList.size()>0){
        if (!CollectionUtils.isEmpty(treeNodeList)) {
            //循环
            for (TreeNode treeNode : treeNodeList) {
                //判断是否一级菜单
                if (treeNode.getParentId() == 0) {
                    //把一级菜单直接放入返回结果集
                    returnTreeNodeList.add(treeNode);
                    //查询绑定子节点
                    bindChildren(treeNode, treeNodeList);
                }
            }
        }
        return returnTreeNodeList;
    }

    @Override
    public List<TreeNode> queryTreeDataByUserId(int userId) {
        //查询数据  所有数据
        List<TreeNode> treeNodeList = menuDao.queryMenuByUserId(userId);
        //定义返回集合
        List<TreeNode> returnTreeNodeList = new ArrayList<>();
        //判断
        //if(treeNodeList!=null&&treeNodeList.size()>0){
        if (!CollectionUtils.isEmpty(treeNodeList)) {
            //循环
            for (TreeNode treeNode : treeNodeList) {
                //判断是否一级菜单
                if (treeNode.getParentId() == 0) {
                    //把一级菜单直接放入返回结果集
                    returnTreeNodeList.add(treeNode);
                    //查询绑定子节点
                    bindChildren(treeNode, treeNodeList);
                }
            }
        }
        return returnTreeNodeList;
    }


    /**
     * 递归绑定孩子方法
     *
     * @param currentTreeNode
     * @param treeNodeList
     */
    private void bindChildren(TreeNode currentTreeNode, List<TreeNode> treeNodeList) {
        //循环判断所有节点中并判断谁是当前节点的孩子
        for (TreeNode treeNode : treeNodeList) {
            //判断循环节点的父id是否和当前节点的id是否相等
            if (treeNode.getParentId() == currentTreeNode.getId()) {
                //获取到当前节点的孩子集合
                List<TreeNode> children = currentTreeNode.getChildren();
                //判断是否为null
                if (children == null) {
                    //为null，就实例化孩子集合
                    children = new ArrayList<>();
                }
                //添加循环节点到当前节点的孩子集合中
                children.add(treeNode);
                //设置回去
                currentTreeNode.setChildren(children);
                //自己再次调用自己，为当前循环节点继续找孩子  递归
                bindChildren(treeNode, treeNodeList);
            }
        }
    }

    @Override
    public List<String> queryButtonPermsByUserId() {
        //根据request获取header中token
        //String token =    request.getHeader("token");
        String token = request.getHeader(ConstantUtil.UserInfoRef.TOKEN_PARAM_NAME);
        //从token载荷获取用户编号
        //  String userId = JwtUtils.getPayloadByToken(token).get("userId").toString();
        String userId = JwtUtils.getPayloadByToken(token).get(ConstantUtil.UserInfoRef.TOKEN_PARAM_USER_ID).asString().replace("\"", "");

        return menuDao.queryButtonPermsByUserId(Integer.valueOf(userId));
    }
}
